/**
	
                 Springs On Rails
                 Copyright [2007] [Sunny Liu]

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.	
*/
package org.springonrails.model;

public class Attribute 
{
	private String javaName;
	private String javaType;	
	private String columnName;
	private String sqlType;
	private String viewLabel;	
	
	private boolean primaryKey;	
	private boolean foreignKey;
	private boolean allowNull;
	private String foreignKeyTableName;
	private String foreignKeyColumnName;
	private String formatString;
	
	
	private String setter;
	private String getter;
	
	public String toString()
	{
		StringBuffer buffer = new StringBuffer();
		buffer.append("<property javaName=\"").append(javaName).append("\"");
		if(javaType!=null) buffer.append("  javaType=\"").append(javaType).append("\"");
		if(columnName!=null) buffer.append("  columnName=\"").append(columnName).append("\"");		
		if(sqlType!=null) buffer.append("  sqlType=\"").append(sqlType).append("\"");		
		if(viewLabel!=null) buffer.append("  viewLabel=\"").append(viewLabel).append("\"");		
		if(formatString!=null) buffer.append("  formatString=\"").append(formatString).append("\"");	
		if(primaryKey) buffer.append("  primaryKey=\"").append(primaryKey).append("\"");
		if(allowNull) buffer.append("  allowNull=\"").append(allowNull).append("\"");
		
		if(getter!=null || setter!=null){
			buffer.append(">\r\n");
			if(getter!=null){
			  buffer.append("<get><![CDATA[\r\n");
			  buffer.append(getter);
			  buffer.append("]]></get>\r\n");
			}
			
			if(setter!=null){
			  buffer.append("<set><![CDATA[\r\n");
			  buffer.append(setter);
			  buffer.append("]]></set>\r\n");
			}
			buffer.append("</property>\r\n");
		}else{
			buffer.append("/>\r\n");
		}
		return buffer.toString();
	}
	
	
	public boolean isDateType()
	{
		if(javaType==null) return false;		
		return javaType.endsWith("Date") 
				|| javaType.endsWith("Timestamp")
				|| javaType.endsWith("Time");
	}
	
	public boolean isHasFormatString()
	{
		return getFormatString()!=null;
	}
	
	
	
	public String getFormatString() {
		if (isDateType() && formatString==null)
			formatString = "MM/dd/yyyy HH:mm:ss";
		return formatString;
	}

	public void setFormatString(String formatString) {
		
		this.formatString = formatString;
	}

	public boolean isUsedInJava()
	{
		return (this.javaName!=null && this.javaType!=null);
	}
	
	public boolean isUsedInSql()
	{
		return (this.sqlType!=null);
	}
	
	public boolean isHasGetter()
	{
		return (this.getter!=null);
	}
	
	public boolean isHasSetter()
	{
		return (this.setter!=null);
	}
	
	
	public String getSetter() {
		return setter;
	}

	public void setSetter(String setter) {
		this.setter = setter;
	}

	public String getGetter() {
		return getter;
	}

	public void setGetter(String getter) {
		this.getter = getter;
	}

	public String getViewLabel() {		
		return (viewLabel==null || viewLabel.trim().length()==0)?this.javaName:viewLabel;
	}

	public void setViewLabel(String viewLabel) {
		this.viewLabel = viewLabel;
	}

	public boolean isForeignKey() {
		return foreignKey;
	}

	public void setForeignKey(boolean foreignKey) {
		this.foreignKey = foreignKey;
	}

	public boolean isNumberType()
	{
		return (javaType.equalsIgnoreCase("int")
				|| javaType.equalsIgnoreCase("integer")
				|| javaType.equalsIgnoreCase("float")
				|| javaType.equalsIgnoreCase("double")
				|| javaType.equalsIgnoreCase("long")	
				|| javaType.equalsIgnoreCase("byte")
				|| javaType.equalsIgnoreCase("short")
		);
	}
	
	public boolean isBooleanType()
	{
		return (javaType.equalsIgnoreCase("boolean"));
	}	
	
	
	
	public boolean isVarcharType()
	{
		return (sqlType.equalsIgnoreCase("varchar")
				|| sqlType.equalsIgnoreCase("varchar2")
				|| sqlType.equalsIgnoreCase("char"));
	}
	
	
	public String getSetterGetterName()
	{
		return javaName.toUpperCase().charAt(0) + javaName.substring(1);
	}
	
	public String getJavaName(){
		return javaName;
	}
	public void setJavaName(String javaName) {
		this.javaName = javaName;
	}
	
	public String getColumnName() 
	{		
		return columnName==null?javaName:columnName;
	}
	
	public void setColumnName(String columnName) {
		this.columnName = columnName;
	}
	public boolean isPrimaryKey() {
		return primaryKey;
	}
	public void setPrimaryKey(boolean primaryKey) {
		this.primaryKey = primaryKey;
	}
	public String getForeignKeyTableName() {
		return foreignKeyTableName;
	}
	public void setForeignKeyTableName(String foreignKeyTableName) {
		this.foreignKey = true;
		this.foreignKeyTableName = foreignKeyTableName;
	}
	public String getForeignKeyColumnName() {
		return foreignKeyColumnName;
	}
	public void setForeignKeyColumnName(String foreignKeyColumnName) {
		this.foreignKeyColumnName = foreignKeyColumnName;
	}
	public boolean isAllowNull() {
		return allowNull;
	}
	public void setAllowNull(boolean allowNull) {
		this.allowNull = allowNull;
	}
	public String getJavaType() 
	{
		//System.out.println(javaName + "<>" + javaType);
		return javaType;
	}
	
	public void setJavaType(String javaType) {		
		this.javaType = javaType;
	}
	public String getSqlType() {
		return sqlType;
	}
	public void setSqlType(String sqlType) {
		this.sqlType = sqlType;
	}
	
}
